<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include '../Module/TemplateModule.php';
include '../Module/OrganizationModule.php';
include '../Module/CategoryModule.php';
$caModule=new CategoryModule();
$orModule=new OrganizationModule();
$dtca=$caModule->getCategorys($_type);
$dtor=$orModule->getOrganizationAll($_type);

$itemModule=new TemplateModule();
if(isset($_REQUEST["id"])){
$id= $_REQUEST["id"];
$item=$itemModule->getTemplate($id);
}
?>
<div style="height: 40px;width: 100%;text-align: center">
    <h1><?=isset($id)?"Edit Item : ":"New Item"?><?=$item["name"]?></h1>
</div>
<div style="width: 100%">
    <table class="w100 dt">
        <tr>
            <th>ID</th>
            <td><label id="txtidi"><?=$item["id"]?></label></td>
        </tr>
        <tr>
            <th>Name</th>
            <td><input id="txtnamei" class="w100" type="text" value="<?=$item["name"]?>"/></td>
        </tr>
        <tr>
            <th>Category</th>
            <td>
                <select id="cbxcategoryi">
                    <?
                    foreach ($dtca as $row){
                        $lv_cur=count(explode(".", $row["level"]));
                        $sp="";
                        if($lv_cur==3)$sp="&nbsp;&nbsp;&nbsp;&nbsp;";
                        if($lv_cur==4)$sp="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        if($lv_cur==5)$sp="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        if($row["id"]==$item["category"])
                            echo "<option selected=1 value='".$row["id"]."'>".$sp.$row["name"]."</option>";
                        else
                            echo "<option value='".$row["id"]."'>".$sp.$row["name"]."</option>";


                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <th>Organization</th>
            <td>
                <select id="cbxorganizationi">
                    <?
                    foreach ($dtor as $row){
                        if($row["id"]==$item["organization"])
                            echo "<option selected=1 value='".$row["id"]."'>".$row["name"]."</option>";
                        else
                            echo "<option value='".$row["id"]."'>".$row["name"]."</option>";


                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <th>Price</th>
            <td><input id="txtprice" class="w100" type="text" value="<?=$item["price"]?>"/></td>
        </tr>
        <tr>
            <th>Amount</th>
            <td><input id="txtamount" class="w100" type="text" value="<?=$item["amount"]?>"/></td>
        </tr>
        <tr>
            <th>Title</th>
            <td><input id="txttitlei" class="w100" type="text" value="<?=$item["title"]?>"/></td>
        </tr>
        <tr>
            <th>Home Page</th>
            <td><input id="txthomepagei" class="w100" type="text" value="<?=$item["homepage"]?>"/></td>
        </tr>
        <tr>
            <th>Website1</th>
            <td><input id="txtweb1i" class="w100" type="text" value="<?=$item["website1"]?>"/></td>
        </tr>
        <tr>
            <th>Image</th>
            <td><input id="txtimagei" class="w100" type="text" value="<?=$item["img"]?>"/></td>
        </tr>
        <tr>
            <th>Image1</th>
            <td><input id="txtimg1" class="w100" type="text" value="<?=$item["img1"]?>"/></td>
        </tr>
        <tr>
            <th>Image2</th>
            <td><input id="txtimg2" class="w100" type="text" value="<?=$item["img2"]?>"/></td>
        </tr>
        <tr>
            <th>Image3</th>
            <td><input id="txtimg3" class="w100" type="text" value="<?=$item["img3"]?>"/></td>
        </tr>
        <tr>
            <th>Image4</th>
            <td><input id="txtimg4" class="w100" type="text" value="<?=$item["img4"]?>"/></td>
        </tr>
        <tr>
            <th>Image5</th>
            <td><input id="txtimg5" class="w100" type="text" value="<?=$item["img5"]?>"/></td>
        </tr>
        <tr>
            <th>Brief</th>
            <td><textarea id="txtbrief" rows="3" class="w100"><?=$item["brief"]?></textarea></td>
        </tr>
        <tr>
            <th>Description</th>
            <td><textarea rows="5" id="txtdes" class="w100"><?=$item["description"]?></textarea></td>
        </tr>
        <tr>
            <th>Insert</th>
            <td style="color:blue"><?=$item["insert"]?></td>
        </tr>
        <?
        if(isset($item["update"])){
        ?>
        <tr>
            <th>Update</th>
            <td style="color: orange"><?=$item["update"]?></td>
        </tr>
        <?
        }
        if(isset($item["delete"])){
        ?>
        <tr>
            <th>Delete</th>
            <td style="color:red"><?=$item["delete"]?></td>
        </tr>
        <?
        }
        ?>
    </table>
    
    <div class="icon32 hover50" title="Save"
         onclick="<?=isset($id)?(isset($item["delete"])?"alert('This item have been removed.');":"update();"):"save()"?>"
         >
        <img src="../images/icon/tick_.png"/>
    </div>
    <div class="icon32 hover50" onclick="<?=isset($id)?"loaditem('$id')":"newitem();"?>" title="Refresh">
        <img class="" src="../images/icon/refresh1.png"/>
    </div>
    <?
    if(isset($id)){
    ?>
    <div class="icon32 hover50" onclick="newitem()" title="Go to New Item">
        <img class="" src="../images/icon/add2.png"/>
    </div>
    <?
    }
    ?>
    <span id="warningmsg"></span>
</div>
<div class="box-menu hover50">
    <div>
    <div class="icon32 hover50" title="Back" onclick="hidebox2()">
        <img src="../images/icon/left.png"/>
    </div>  
    
    <div class="icon32 hover50" title="Save"
         onclick="<?=isset($id)?(isset($item["delete"])?"alert('This item have been removed.');":"update();"):"save()"?>"
         >
        <img src="../images/icon/tick_.png"/>
    </div>
    <div class="icon32 hover50" onclick="<?=isset($id)?"loaditem('$id')":"newitem();"?>" title="Refresh">
        <img class="" src="../images/icon/refresh1.png"/>
    </div>
    <?
    if(isset($id)){
    ?>
    <div class="icon32 hover50" onclick="newitem()" title="Go to New Item">
        <img class="" src="../images/icon/add2.png"/>
    </div>
    <?
    }
    ?>
    </div>
</div>
<script>
    function save(){
        process("add");
    }
    function update(){
        process("edit");
    }
    function process(action){
        if(!_FcheckFilled($("#txtnamei").val())){$("#txtnamei").focus();return;}
        if(!_FcheckFilled($("#txtimagei").val())){$("#txtimagei").focus();return;}
        if(!_FcheckFilled($("#txttitlei").val())){$("#txttitlei").focus();return;}
        if(!_FcheckFilled($("#txtprice").val())){$("#txtprice").focus();return;}
        if(!_FcheckNumber($("#txtprice").val())){$("#txtprice").focus();return;}
        if(!_FcheckFilled($("#txtamount").val())){$("#txtamount").focus();return;}
        if(!_FcheckNumber($("#txtamount").val())){$("#txtamount").focus();return;}
        var surl="../Controller/";
        var sdata={
                "controller":"TemplateController",
                "action":action,
                "_type":"<?=$_type?>",
                "id":$("#txtidi").html(),
                "name":$("#txtnamei").val(),
                "category":$("#cbxcategoryi").val(),
                "organization":$("#cbxorganizationi").val(),
                "image":$("#txtimagei").val(),
                "img1":$("#txtimg1").val(),
                "img2":$("#txtimg2").val(),
                "img3":$("#txtimg3").val(),
                "img4":$("#txtimg4").val(),
                "img5":$("#txtimg5").val(),
                "price":$("#txtprice").val(),
                "amount":$("#txtamount").val(),
                "title":$("#txttitlei").val(),
                "brief":$("#txtbrief").val(),
                "des":$("#txtdes").val(),
                "homepage":$("#txthomepagei").val(),
                "website1":$("#txtweb1i").val()
                
        };
        jQuery.ajax({
                type:"POST", 
                data:sdata, 
                dataType:"text", 
                url:surl, 
                success: function (data){
                    if(data=="true"){
                        $("#warningmsg").html("Sucessful. Form will reset in 3 second.");
                        $("#warningmsg").show();
                        setTimeout(function() {
                            $("#warningmsg").hide();
                        },3000);
                    }else{
                        $("#warningmsg").html(data);
                        $("#warningmsg").show();
                    }

                },
                error: function (xhr, ajaxOptions, thrownError){
                        $("#warningmsg").html("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
                        $("#warningmsg").show();
                }
        });

    }
</script>